package at.ac.tuwien.sepm.s2011.e0208329.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.apache.log4j.Logger;

import at.ac.tuwien.sepm.s2011.e0208329.dao.DAOFactoryImpl;
import at.ac.tuwien.sepm.s2011.e0208329.dao.OrderDAOImpl;
import at.ac.tuwien.sepm.s2011.e0208329.domain.Dienstleistung;
import at.ac.tuwien.sepm.s2011.e0208329.services.MyCarServices;
import at.ac.tuwien.sepm.s2011.e0208329.services.MyCarServicesImpl;

@SuppressWarnings("serial")
public class CreateServiceUI extends JFrame {
	private JTextField[] fields;
	private String[] labels;
	private JPanel labelPanel;
	private JPanel fieldPanel;
	private static final Logger log = Logger.getLogger(CreateServiceUI.class);

	public CreateServiceUI(String name) {
		super(name);
		// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		labelPanel = new JPanel(new GridLayout(5, 1));
		fieldPanel = new JPanel(new GridLayout(5, 1));
		fields = new JTextField[5];
		labels = new String[5];
		labels[0] = "Name";
		labels[1] = "Preis";
		labels[2] = "MwSt";
		labels[3] = "Techniker";
		labels[4] = "Aufwand";

		for (int i = 0; i < labels.length; i++) {
			fields[i] = new JTextField();
			fields[i].setColumns(25);
			JLabel lab = new JLabel(labels[i], JLabel.RIGHT);
			lab.setLabelFor(fields[i]);
			labelPanel.add(lab);
			JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
			p.add(fields[i]);
			fieldPanel.add(p);
		}

		JButton submit = new JButton("Neue Dienstleistung speichern");
		submit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Dienstleistung ser;
				boolean ok = false;
				try {
					ser = new Dienstleistung(fields[0].getText(), Double
							.parseDouble(fields[1].getText()), Double
							.parseDouble(fields[2].getText()), fields[3]
							.getText(), Integer.parseInt(fields[4].getText())); //read the entries
					if (ser != null) {
						//DAOFactoryImpl.getDAOFactory().getServiceDAO().insertService(ser);
						MyCarServicesImpl.getInstance().createService(ser); // do the insert
					}
					ok = true;
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(null,
							"Invalid data format !!!", "Data input error",
							JOptionPane.ERROR_MESSAGE);
				} catch (SQLException e2) {
					log.error(e2.getMessage());
					JOptionPane.showMessageDialog(null, "Database error",
							"Please see log file for more detail !",
							JOptionPane.ERROR_MESSAGE);
				}
				if (ok) {
					for (int i = 0; i < fields.length; i++) {
						fields[i].setText("");
					}
				}
			}
		});

		JPanel buttonPanel = new JPanel();
		buttonPanel.add(submit);
		this.getContentPane().add(labelPanel, BorderLayout.WEST);
		this.getContentPane().add(fieldPanel, BorderLayout.CENTER);
		this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
		this.pack();
	}

}
